<?
$smarty=new Smarty();
if(!ANTIHACK) die("ANTIHACK...");
include("../includes/language_vi.php");
include_once("./includes/getlistMenu.php");
if($_GET["ajax"]=="gettags")
{
	if($_GET["code"]!="") $w=" where code like '%$_GET[code]%'";
	$q=query("select * from tags $w");
	$data="";
	while($r=fetch($q))
	{
		$data[]=$r["code"];
	}
	echo json_encode($data);
	exit;
}
$smarty->assign("nameact","Đăng bài viết");
if(isset($_POST['newpost']))
{
	$title=addslashes($_POST['title']);
	$menuid = $_POST['menuid'];
	$described = addslashes($_POST['described']);
	$is_active = $_POST['is_active'];
	$image = $_POST['image'];
	$content = $_POST['content'];
	$userID=$_SESSION['id'];
	$tags=$_POST['hidden-tags'];
	$tags=str_replace("'","`",$tags);
	$tags=str_replace("\"","`",$tags);
		$arr_tags=str_replace(",","'),('",$tags);
		$q=query("INSERT IGNORE INTO tags (code) VALUES ('$arr_tags');");
	$trailer=$_POST['trailer'];
	
		$size = getimagesize($image);
	$width			= $size[0];
	$height			= $size[1];
	
	
		if($title=="")
			$smarty->assign("msg",$msg["title_empty"]);
		else if($described=="")
			$smarty->assign("msg",$msg["described_empty"]);
		else if($content=="")
			$smarty->assign("msg",$msg["content_empty"]);
		else
		{
			if($is_active!=1)
				$is_active=0;
			$data=array(
				"userid" => $userID,
				"parent" => $menuid,
				"title" => $title,
				"time" => time(),
				"image" => $image,
				"imgheight" =>$height,
				"imgwidth" => $width,
				"described" => $described,
				"content" => $content,
				"is_active"=>$is_active,
				"trailer"=>$trailer,
				"tags" => $tags
			);
			$ret = insert($data,"post");
			if($ret)
				$smarty->assign("msg",$msg["post_success"]."<script>setTimeout('location.href=\"".base_admin."?act=manager-post\"',1000);</script>");
			else
				$smarty->assign("msg",$msg["error"]);
		}
	$_POST["tags"]=str_replace(",",'","',$_POST["tags"]);
	$smarty->assign("data",$_POST);
}
$per=query("select `group`.`permission` as permission from user,`group` where `user`.`id`=$_SESSION[id] and `user`.`group` = `group`.`id`");
	$fper=fetch($per);
	$Aper=json_decode($fper['permission'],true);
$menu=getlistMenu("menuid",$_POST["menuid"],"box",$Aper);

$smarty->assign("menu",$menu);
$smarty->display("new-post.htm");
?>